<& /Elements/Header, Title=>"On-Call Tracker" &>
<& /OnCall/Elements/Tabs,
    current_toptab => '/OnCall/index.html',
    Title => "On-Call Tracker" &>

<& /Elements/ListActions, actions => \@Actions &>


% foreach my $group (@groups) {
<H2><% $group->Name %></H2>
% my $content = $group->FirstAttribute('GroupPhone');
% $content = ref($content) ? $content->Content : $content;
<H3>Group Phone: <% $content %></H3>
<table border=1>
<tr>
<th>On Call</th> <th>Name</th> <th>Work</th> <th>Cell</th> <th>Home</th>
</tr>
%   my %seen;
%   foreach my $type qw( TeamLead Primary Secondary ) {
%     foreach my $user (@{$users{$group->Name}{$type}}) {
%     $seen{$user->Name} = 1;
<tr>
<td><%$type%></td>
<td><%$user->RealName%> (<a href="/Admin/Users/Modify.html?id=<%$user->id%>"><%$user->Name%></a>)</td>
<td><%$user->WorkPhone%></td>
<td><%$user->MobilePhone%></td>
<td><%$user->HomePhone%></td>
</tr>
%     }
%   }
%   my $AllMembers = $group->UserMembersObj;
%   while ( my $user = $AllMembers->Next) {
%       next if $seen{$user->Name};
<tr>
<td>Member</td>
<td><%$user->RealName%> (<a href="/Admin/Users/Modify.html?id=<%$user->id%>"><%$user->Name%></a>)</td>
<td><%$user->WorkPhone%></td>
<td><%$user->MobilePhone%></td>
<td><%$user->HomePhone%></td>
</tr>
%   }
</table> <a href="Modify.html?id=<%$group->id%>">[update <%$group->Name%>]</a><P>
% }

<%INIT>
my @groups;
my %users;
my @Actions;

my $group = RT::Group->new($session{CurrentUser});

if ($ARGS{group}) {

    my (@primary, @secondary, @teamlead);
    my ($rv, $msg) = $group->Load($ARGS{group});
    unless ($rv) {
        $m->comp("/Elements/Error", Why => $msg);
        $m->abort;
    }

    unless ($group->CurrentUserHasRight('AdminOnCall')) {
        $m->comp("/Elements/Error", Why => 'No permission to modify OnCall information for group '. $group->Name);
        $m->abort;
    }

    foreach my $arg (keys %ARGS) {

        next unless $arg =~ /^User-(\d+)/;
        my $id = $1;
        push(@primary,   $id) if $ARGS{$arg} eq 'primary';
        push(@secondary, $id) if $ARGS{$arg} eq 'secondary';
        push(@teamlead,  $id) if $ARGS{$arg} eq 'teamlead';
    }
    $group->SetPrimaryOnCall(@primary);
    $group->SetSecondaryOnCall(@secondary);
    $group->SetTeamLead(@teamlead);

    $group->SetAttribute(Name => 'GroupPhone', Content => $ARGS{'GroupPhone'});
}

my ($rv, $msg) = $group->LoadUserDefinedGroup("On Call Groups");
my $members = $group->MembersObj;
$members->LimitToGroups;
while (my $groupmember = $members->Next) {
    push @groups, $groupmember->MemberObj->Object;
}

foreach my $OCGroup (@groups) {
    $users{$OCGroup->Name}{Primary} = [ $OCGroup->PrimaryOnCallUsers ];
    $users{$OCGroup->Name}{Secondary} = [ $OCGroup->SecondaryOnCallUsers ];
    $users{$OCGroup->Name}{TeamLead} = [ $OCGroup->TeamLeadUsers ];
}
</%INIT>
<%ARGS>
</%ARGS>
